<template>
	<view class="register-container">
		<!-- 背景装饰 -->
		<view class="background-decoration"></view>
		
		<!-- 注册卡片 -->
		<view class="register-card">
			<!-- Logo和标题 -->
			<view class="register-header">
				<view class="logo-container">
					<text class="logo-icon">🌱</text>
				</view>
				<text class="app-title">闪联智圃</text>
				<text class="app-subtitle">用户注册</text>
			</view>
			
			<!-- 注册表单 -->
			<view class="register-form">
				<!-- 用户名输入 -->
				<view class="input-group">
					<view class="input-icon">👤</view>
					<input 
						class="form-input" 
						v-model="username" 
						placeholder="请输入用户名(3-20个字符)" 
						type="text"
						maxlength="20"
						@input="clearError"
					/>
				</view>
				
				<!-- 昵称输入 -->
				<view class="input-group">
					<view class="input-icon">😊</view>
					<input 
						class="form-input" 
						v-model="nickname" 
						placeholder="请输入昵称(可选)" 
						type="text"
						maxlength="20"
						@input="clearError"
					/>
				</view>
				
				<!-- 邮箱输入 -->
				<view class="input-group">
					<view class="input-icon">📧</view>
					<input 
						class="form-input" 
						v-model="email" 
						placeholder="请输入邮箱(可选)" 
						type="email"
						@input="clearError"
					/>
				</view>
				
				<!-- 密码输入 -->
				<view class="input-group">
					<view class="input-icon">🔒</view>
					<input 
						class="form-input" 
						v-model="password" 
						placeholder="请输入密码(6-20个字符)" 
						:password="!showPassword"
						maxlength="20"
						@input="clearError"
					/>
					<view class="password-toggle" @click="togglePassword">
						<text class="toggle-icon">{{ showPassword ? '🙈' : '👁️' }}</text>
					</view>
				</view>
				
				<!-- 确认密码输入 -->
				<view class="input-group">
					<view class="input-icon">🔐</view>
					<input 
						class="form-input" 
						v-model="confirmPassword" 
						placeholder="请确认密码" 
						:password="!showConfirmPassword"
						maxlength="20"
						@input="clearError"
					/>
					<view class="password-toggle" @click="toggleConfirmPassword">
						<text class="toggle-icon">{{ showConfirmPassword ? '🙈' : '👁️' }}</text>
					</view>
				</view>
				
				<!-- 用户类型提示 -->
				<view class="user-type-tip">
					<text class="tip-icon">ℹ️</text>
					<text class="tip-text">注册后将成为普通用户，管理员账户请联系系统管理员</text>
				</view>
				
				<!-- 错误提示 -->
				<view v-if="errorMessage" class="error-message">
					<text class="error-icon">⚠️</text>
					<text class="error-text">{{ errorMessage }}</text>
				</view>
				
				<!-- 注册按钮 -->
				<button 
					class="register-btn" 
					:class="{ 'loading': isLoading }"
					@click="handleRegister"
					:disabled="isLoading"
				>
					<text v-if="!isLoading" class="btn-text">注册</text>
					<text v-else class="btn-text">注册中...</text>
				</button>
				
				<!-- 登录链接 -->
				<view class="login-link" @click="goToLogin">
					<text class="link-text">已有账户？点击登录</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			username: '',
			nickname: '',
			email: '',
			password: '',
			confirmPassword: '',
			showPassword: false,
			showConfirmPassword: false,
			errorMessage: '',
			isLoading: false
		}
	},
	
	methods: {
		// 切换密码显示状态
		togglePassword() {
			this.showPassword = !this.showPassword;
		},
		
		// 切换确认密码显示状态
		toggleConfirmPassword() {
			this.showConfirmPassword = !this.showConfirmPassword;
		},
		
		// 清除错误信息
		clearError() {
			this.errorMessage = '';
		},
		
		// 表单验证
		validateForm() {
			if (!this.username.trim()) {
				this.errorMessage = '请输入用户名';
				return false;
			}
			
			if (this.username.length < 3) {
				this.errorMessage = '用户名至少3个字符';
				return false;
			}
			
			if (!/^[a-zA-Z0-9_]{3,20}$/.test(this.username)) {
				this.errorMessage = '用户名只能包含字母、数字和下划线';
				return false;
			}
			
			if (!this.password.trim()) {
				this.errorMessage = '请输入密码';
				return false;
			}
			
			if (this.password.length < 6) {
				this.errorMessage = '密码至少6个字符';
				return false;
			}
			
			if (!this.confirmPassword.trim()) {
				this.errorMessage = '请确认密码';
				return false;
			}
			
			if (this.password !== this.confirmPassword) {
				this.errorMessage = '两次输入的密码不一致';
				return false;
			}
			
			if (this.email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.email)) {
				this.errorMessage = '邮箱格式不正确';
				return false;
			}
			
			return true;
		},
		
		// 处理注册
		async handleRegister() {
			if (!this.validateForm()) {
				return;
			}
			
			this.isLoading = true;
			this.clearError();
			
			try {
				// 调用注册云函数
				const result = await uniCloud.callFunction({
					name: 'userRegister',
					data: {
						username: this.username.trim(),
						nickname: this.nickname.trim(),
						email: this.email.trim(),
						password: this.password,
						confirmPassword: this.confirmPassword
					}
				});
				
				console.log('注册结果:', result);
				
				if (result.result.code === 0) {
					// 注册成功
					uni.showToast({
						title: '注册成功',
						icon: 'success',
						duration: 2000
					});
					
					// 延迟跳转到登录页面
					setTimeout(() => {
						uni.navigateBack();
					}, 2000);
					
				} else {
					// 注册失败
					this.errorMessage = result.result.message || '注册失败，请稍后重试';
				}
				
			} catch (error) {
				console.error('注册错误:', error);
				this.errorMessage = '网络错误，请稍后重试';
			} finally {
				this.isLoading = false;
			}
		},
		
		// 跳转到登录页面
		goToLogin() {
			uni.navigateBack();
		}
	}
}
</script>

<style>
/* 苹果设计风格 - 注册页面 */
.register-container {
	min-height: 100vh;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 40rpx;
	position: relative;
	font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Helvetica Neue', sans-serif;
}

.background-decoration {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="20" cy="20" r="2" fill="rgba(255,255,255,0.1)"/><circle cx="80" cy="80" r="2" fill="rgba(255,255,255,0.1)"/><circle cx="40" cy="60" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="60" cy="40" r="1" fill="rgba(255,255,255,0.1)"/></svg>') repeat;
	animation: float 20s ease-in-out infinite;
}

@keyframes float {
	0%, 100% { transform: translateY(0px); }
	50% { transform: translateY(-20px); }
}

.register-card {
	width: 100%;
	max-width: 400px;
	background: rgba(255, 255, 255, 0.95);
	border-radius: 24rpx;
	padding: 60rpx 40rpx;
	box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.1);
	backdrop-filter: blur(20rpx);
	border: 1px solid rgba(255, 255, 255, 0.2);
	position: relative;
	z-index: 1;
}

.register-header {
	text-align: center;
	margin-bottom: 60rpx;
}

.logo-container {
	width: 120rpx;
	height: 120rpx;
	margin: 0 auto 30rpx;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	border-radius: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 10rpx 30rpx rgba(102, 126, 234, 0.3);
}

.logo-icon {
	font-size: 60rpx;
}

.app-title {
	font-size: 48rpx;
	font-weight: 700;
	color: #333;
	display: block;
	margin-bottom: 10rpx;
	letter-spacing: -0.5px;
}

.app-subtitle {
	font-size: 28rpx;
	color: #666;
	display: block;
	font-weight: 400;
}

.register-form {
	display: flex;
	flex-direction: column;
	gap: 30rpx;
}

.input-group {
	position: relative;
	display: flex;
	align-items: center;
	background: #f8f9fa;
	border-radius: 16rpx;
	border: 2rpx solid transparent;
	transition: all 0.3s ease;
}

.input-group:focus-within {
	border-color: #667eea;
	background: #fff;
	box-shadow: 0 0 0 6rpx rgba(102, 126, 234, 0.1);
}

.input-icon {
	width: 50rpx;
	height: 50rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 32rpx;
	margin-left: 20rpx;
	color: #999;
}

.form-input {
	flex: 1;
	height: 100rpx;
	padding: 0 20rpx;
	font-size: 32rpx;
	color: #333;
	background: transparent;
	border: none;
	outline: none;
}

.form-input::placeholder {
	color: #999;
	font-size: 28rpx;
}

.password-toggle {
	width: 80rpx;
	height: 100rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	cursor: pointer;
}

.toggle-icon {
	font-size: 32rpx;
	color: #999;
	transition: color 0.3s ease;
}

.password-toggle:active .toggle-icon {
	color: #667eea;
}

.user-type-tip {
	display: flex;
	align-items: center;
	gap: 15rpx;
	padding: 20rpx;
	background: rgba(102, 126, 234, 0.1);
	border-radius: 12rpx;
	margin: 10rpx 0;
}

.tip-icon {
	font-size: 28rpx;
}

.tip-text {
	font-size: 24rpx;
	color: #667eea;
	line-height: 1.4;
}

.error-message {
	display: flex;
	align-items: center;
	gap: 15rpx;
	padding: 20rpx;
	background: rgba(255, 59, 48, 0.1);
	border-radius: 12rpx;
	margin: 10rpx 0;
}

.error-icon {
	font-size: 28rpx;
}

.error-text {
	font-size: 26rpx;
	color: #ff3b30;
	line-height: 1.4;
}

.register-btn {
	height: 100rpx;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	border-radius: 16rpx;
	border: none;
	color: white;
	font-size: 32rpx;
	font-weight: 600;
	margin-top: 20rpx;
	transition: all 0.3s ease;
	box-shadow: 0 8rpx 24rpx rgba(102, 126, 234, 0.3);
}

.register-btn:active {
	transform: translateY(2rpx);
	box-shadow: 0 4rpx 12rpx rgba(102, 126, 234, 0.3);
}

.register-btn.loading {
	opacity: 0.7;
	transform: none;
}

.btn-text {
	color: white;
	font-size: 32rpx;
	font-weight: 600;
}

.login-link {
	text-align: center;
	margin-top: 40rpx;
	padding: 20rpx;
}

.link-text {
	font-size: 28rpx;
	color: #667eea;
	text-decoration: underline;
}
</style>